/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2025-02-20 17:35:13
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-02-26 08:40:27
 * @FilePath: /vue3-admin-server/src/admin/v1/auth/user.module.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Body, Controller, Post, Param, Get } from '@nestjs/common';

import { UserListDTO, UserDTO } from 'src/common/dto';
import { UserService } from 'src/shareModules/auth/auth.module';
import { ResponseService } from 'src/shareModules/common/common.module';
import { generateObjFromError } from 'src/common/utils';

@Controller('admin/api/v1/user')
export class UserController {
    constructor(
        private readonly userService: UserService,
        private readonly responseService: ResponseService
    ) {}

    @Post('readList')
    async readList(
        @Body() userListDto: UserListDTO
    ): Promise<VO.ResultVO<VO.PageDataVO<VO.UserVO>>> {
        const result = await this.userService.readList(userListDto);
        return this.responseService.success(undefined, result);
    }

    @Get('readDetail/:id')
    async readDetail(
        @Param() { id }: { id: number }
    ): Promise<VO.ResultVO<VO.UserVO | null>> {
        const menuDetail = await this.userService.readDetail(id);
        return this.responseService.success(undefined, menuDetail);
    }

    @Post('save')
    async save(@Body() userDTO: UserDTO): Promise<VO.ResultVO<unknown>> {
        try {
            const id = await this.userService.save(userDTO);
            return this.responseService.success(undefined, { id });
        } catch (e) {
            return this.responseService.error(
                undefined,
                generateObjFromError(e as Error)
            );
        }
    }
}
